using MediatR;
using Medical.Api.Read.Application.Command.BasicManagement.DeviceClassificationDictionaryCommand;
using Medical.Api.Read.Application.Command.BasicManagement.DeviceDictionaryCommand;
using Medical.Api.Read.Application.Command.BasicManagement.ShelfDictionaryCommand;
using Medical.Api.Read.Application.Command.BasicManagement.EquipmentPackageDictionaryCommand;
using Medical.Api.Read.Dto.BasicManagement.DeviceClassificationDictionaryDto;
using Medical.Api.Read.Dto.BasicManagement.DeviceDictionaryDto;
using Medical.Api.Read.Dto.BasicManagement.ShelfDictionaryDto;
using Medical.Api.Read.Dto.BasicManagement.EquipmentPackageDictionaryDto;
using Medical.Domain.BasicManagement;
using Medical.ErrorCode;
using Microsoft.AspNetCore.Mvc;

namespace Medical.Api.Read.Controllers
{
    /// <summary>
    /// 基础管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class BasicManagementController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<BasicManagementController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        public BasicManagementController(IMediator mediator, ILogger<BasicManagementController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        #region 设备字典

        /// <summary>
        /// 查询设备列表
        /// </summary>
        /// <param name="dto">查询参数</param>
        /// <returns>分页设备列表</returns>
        [HttpGet("QueryEquipment")]
        public ApiResult<ApiPageIng<Equipment>> QueryEquipment([FromQuery] QueryEquipmentDto dto)
        {
            try
            {
                _logger.LogInformation("开始查询设备列表");
                var result = _mediator.Send(new QueryEquipmentCommand(dto)).Result;
                _logger.LogInformation("查询设备列表成功");
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "查询设备列表失败");
                return new ApiResult<ApiPageIng<Equipment>>
                {
                    Code = ApiEnums.Error,
                    Msg = ex.Message
                };
            }
        }

        #endregion

        #region 货架字典

        /// <summary>
        /// 查询货架列表
        /// </summary>
        /// <param name="dto">查询参数</param>
        /// <returns>分页货架列表</returns>
        [HttpGet("QueryShelves")]
        public ApiResult<ApiPageIng<Shelves>> QueryShelves([FromQuery] QueryShelvesDto dto)
        {
            try
            {
                _logger.LogInformation("开始查询货架列表");
                var result = _mediator.Send(new QueryShelvesCommand(dto)).Result;
                _logger.LogInformation("查询货架列表成功");
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "查询货架列表失败");
                return new ApiResult<ApiPageIng<Shelves>>
                {
                    Code = ApiEnums.Error,
                    Msg = ex.Message
                };
            }
        }

        #endregion

        #region 器械字典

        /// <summary>
        /// 查询器械列表
        /// </summary>
        /// <param name="dto">查询参数</param>
        /// <returns>分页器械列表</returns>
        [HttpGet("QueryApparatus")]
        public ApiResult<ApiPageIng<Apparatus>> QueryApparatus([FromQuery] QueryApparatusDto dto)
        {
            try
            {
                _logger.LogInformation("开始查询器械列表");
                var result = _mediator.Send(new QueryApparatusCommand(dto)).Result;
                _logger.LogInformation("查询器械列表成功");
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "查询器械列表失败");
                return new ApiResult<ApiPageIng<Apparatus>>
                {
                    Code = ApiEnums.Error,
                    Msg = ex.Message,
                    Data = new ApiPageIng<Apparatus>()
                };
            }
        }

        #endregion

        #region 器械类型字典

        /// <summary>
        /// 获取器械分类树状结构
        /// </summary>
        /// <param name="status">状态筛选（可选）：0-禁用，1-启用</param>
        /// <returns>器械分类树状结构</returns>
        [HttpGet("GetApparatusTypeTree")]
        public ApiResult<List<ApparatusTypeTreeDto>> GetApparatusTypeTree([FromQuery] int? status = null)
        {
            try
            {
                _logger.LogInformation("开始获取器械分类树状结构");
                var command = new GetApparatusTypeTreeCommand { Status = status };
                var result = _mediator.Send(command).Result;
                _logger.LogInformation("获取器械分类树状结构成功");
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取器械分类树状结构失败");
                return new ApiResult<List<ApparatusTypeTreeDto>>
                {
                    Code = ApiEnums.Error,
                    Msg = ex.Message,
                    Data = new List<ApparatusTypeTreeDto>()
                };
            }
        }

        #endregion

        #region 器械包字典

        /// <summary>
        /// 查询器械包列表
        /// </summary>
        /// <param name="dto">查询参数</param>
        /// <returns>分页器械包列表</returns>
        [HttpGet("QueryEquipmentPackage")]
        public ApiResult<ApiPageIng<EquipmentPackage>> QueryEquipmentPackage([FromQuery] QueryEquipmentPackageDto dto)
        {
            try
            {
                _logger.LogInformation("开始查询器械包列表");
                var result = _mediator.Send(new QueryEquipmentPackageCommand(dto)).Result;
                _logger.LogInformation("查询器械包列表成功");
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "查询器械包列表失败");
                return new ApiResult<ApiPageIng<EquipmentPackage>>
                {
                    Code = ApiEnums.Error,
                    Msg = ex.Message,
                    Data = new ApiPageIng<EquipmentPackage>()
                };
            }
        }

        #endregion
    }
}
